#include "GenerateList.h"

namespace FPTK{

generatelist::generatelist()
{
list=NULL;
element=NULL;
}

generatelist::~generatelist()
{
listfiles *current,*nextelement;
current=list;
nextelement=list;
while(current!=NULL)
   {
   nextelement=current->next;
   delete []current->filename;
   delete current;
   current=nextelement;
   }
}

void generatelist::AddFile(char *_filename)
{
listfiles *current;
current=new listfiles;
current->filename=new char[strlen(_filename)+1];
strcpy(current->filename,_filename);
current->next=NULL;
if(list==NULL)
   {
   list=current;
   element=current;
   }
else
   {
   element->next=current;
   element=element->next;
   }
}

void generatelist::SaveList(char* _filename,bool erase)///if erase=0, append the file, else overwrite or create the file if doesn't exist
{
listfiles *current;
FILE* ptfile;
current=list;
if(erase)
   {
   ptfile=fopen(_filename,"wt");
   }
else
   {
   ptfile=fopen(_filename,"at");
   }
if(ptfile!=NULL)
   {
   while(current!=NULL)
      {
      fputs(current->filename,ptfile);
      fputs("\n",ptfile);
      current=current->next;
      }
   fclose(ptfile);
   }
}

} //end namespace FPTK